package kpetruk.tanks2d.server.data;

import java.util.ArrayList;
import java.util.LinkedList;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kpetruk.tanks2d.bots.BasicBot;

public class GameMap {
  ArrayList<Unit> units = new ArrayList<Unit>();  //tanks
  ArrayList bullets = new ArrayList();
  ArrayList trees = new ArrayList();
  //map bounds
  float minX, minY, maxX, maxY = 0;
  
  World w = null;
  
  //input queue
  LinkedList inputQueue = new LinkedList();
  
  //logger
  Logger log =  LoggerFactory.getLogger("kpetruk.tank2d.server.data.GameMap");
  
  public GameMap() {
    
  }
  
  public void init() {
    w.setGravity(new Vec2(0,0));
    
  }
  
  public void process() {
    //input
    
    
    //units
    for(Unit u : units){
      //ai
      u.useAI();
      
      //movement
      if(u.wantMove == 1) {
        u.speedup();
      } else if (u.wantMove == -1) {
        u.slowdown();
      } else {
        u.stop();
      }
      
      
      
      u.speed = u.wantMove;
      u.rotationSpeed = u.wantRotation;
      u.x = u.x + u.speed;
      
      //movement limitation (e.g. if speed exceed)
      
      //shooting
      
      
      //actions
      
      
      //logging
      //log.debug(u.toString());
    }
    
    //shots
    
    //chat
    
    //map events (flag capture, new round trigger, etc)
    
    //w.step(0.1, 10);
    
  }
  
  public void addTank() {
    
  }
  
  public void defaultInit() {
    
    AABB worldAABB = new AABB(); 
    Vec2 gravity = new Vec2(0,0);
    w = new World(worldAABB, gravity, false);
    
    minX = 2000f;
    minY = 2000f;
    maxX = -2000f;
    maxY = -2000f;
    
    //add tanks
    Unit botTank1 = new Unit();
    botTank1.bot = true;
    botTank1.id = -2;
    botTank1.addControl(new BasicBot());
    BodyDef bd1 = new BodyDef();
    PolygonDef psd = new PolygonDef();
    psd.setAsBox(2f, 4f, new Vec2(0,0),0f);
    psd.density = 1.0f;
    bd1.position = new Vec2(500, 500);
    Body armor1 = w.createBody(bd1);
    armor1.createShape(psd);
    armor1.setMassFromShapes();
    botTank1.body = armor1;
    units.add(botTank1);
    
    Unit botTank2 = new Unit();
    botTank2.bot = true;
    botTank1.id = -3;
    botTank2.addControl(new BasicBot());
    BodyDef bd2 = new BodyDef();
    PolygonDef psd2 = new PolygonDef();
    psd2.setAsBox(2f, 4f, new Vec2(0,0),0f);
    psd2.density = 1.0f;
    bd2.position = new Vec2(-500, -500);
    Body armor2 = w.createBody(bd2);
    armor2.createShape(psd2);
    armor2.setMassFromShapes();
    botTank2.body = armor2;
    units.add(botTank2);
    
  }
  
}
